function MonthController(){ }

MonthController.prototype.freeMemory = function() {
    $('#monthlyList').html('');
};

$(function(){

    var tutorial = false;

    $(document).on('pagebeforeshow', '#pageMonthArchives', function(){        
        monaca.updateUIStyle("titlelable", "text", "月別一覧");
        monaca.updateUIStyle("backheader", "visibility", "true");
        monaca.updateUIStyle("footer", "visibility", "false");
        Util.prototype.changeSnapperElement("month-content");

        $('.tutorial').hide();
        tutorial = true;
        
        BookmarksService.prototype.countProductByMonth(function(results) {
            var len = results.rows.length,   
                list = '',
                year_previousLoop = 0;   
                
            if (len > 0) {
                tutorial = false;
                for (var i=0, year, month; i<len; i++){
                    year = results.rows.item(i).registered_year;
                    month = results.rows.item(i).registered_month;
                    
                    if (i == 0 || year != year_previousLoop) {
                        year_previousLoop = year;
                        list += '<li data-role="list-divider" data-theme="b">' + year + "年</li>";
                    }
                    
                    list += "<li><a href='#' id = 'hiduke" + year.toString() + month.toString() + "' class = 'bookmarkHiduke'>"+
                        year+"年"+month+"月  <span class='ui-li-count'>" + results.rows.item(i).tuki+"</span></a></li>";
                }
            } else {
                list = "<h3>ブックマーク登録がありません</h3>";
            }
            
            $('#monthlyList').append(list).listview('refresh');
            
            Util.prototype.hideSpinner();
        });        
        
    });
    
    $(document).on('pageshow', '#pageMonthArchives', function(){
        if(tutorial){
            $('.tutorial').show("slow");
        }
        PageChangeController.prototype.freeMemory();
    });
    
    // 月別リストクリック
    // クリックされた月のブックマーク一覧を表示
    //$(document).on('touchend click', '.bookmarkHiduke', function(){
    $(document).on('tap', '.bookmarkHiduke', function(){
        var ym = $(this).attr('id');
        var title = ym.slice(6,10) + "年" + ym.slice(10) + "月";
 
        var params = {'title': title, 'fromPage': 'pageMonthArchives','ym': ym};
        PageChangeController.prototype.changeToPage('pageBookmarkList',  params);
    });
});